package cn.zyx.sys.service;

import cn.zyx.common.entity.Page;
import cn.zyx.common.properites.CacheProperties;
import cn.zyx.common.utils.sys.UserUtils;
import cn.zyx.sys.entity.Role;
import cn.zyx.sys.entity.User;
import cn.zyx.sys.mapper.RoleMapper;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 角色管理类
 *
 * @author zyx
 */
@Service
@Transactional(readOnly = true)
public class RoleService {

    @Autowired
    private UserService userService;

    @Autowired
    private RoleMapper roleMapper;

    /**
     * 根据id查询角色信息
     * @param id
     * @return
     */
    public Role get(String id) {
        return roleMapper.get(id);
    }

    public Role getRoleByName(String name) {
        Role r = new Role();
        r.setName(name);
        return roleMapper.getByName(r);
    }

    public Role getRoleByEnname(String enname) {
        Role r = new Role();
        r.setEnname(enname);
        return roleMapper.getByEnname(r);
    }

    /**
     * 查询角色的所有无下属菜单ID
     * @param id
     * @return
     */
    public List<String> queryAllNotChildrenMenuId(String id){
        if(StringUtils.isNotEmpty(id)){
            return roleMapper.queryAllNotChildrenMenuId(id);
        }
        return Lists.newArrayList();
    }

    /**
     * 查询角色的所有菜单ID和按钮id
     * @param id
     * @return
     */
    public List<String> queryAllMenuId(String id){
        if(StringUtils.isNotEmpty(id)){
            return roleMapper.queryAllMenuId(id);
        }
        return Lists.newArrayList();
    }

    public List<Role> findAllRole() {
        return UserUtils.getRoleList();
    }

    @Transactional(readOnly = false)
    public void saveRole(Role role) {
        if (StringUtils.isBlank(role.getId())) {
            role.preInsert();
            roleMapper.insert(role);
        } else {
            role.preUpdate();
            roleMapper.update(role);
        }
        // 更新角色与菜单关联
        roleMapper.deleteRoleMenu(role);
        if (!role.getMenuList().isEmpty()) {
            roleMapper.insertRoleMenu(role);
        }

        // 清除用户角色缓存
        UserUtils.removeCache(CacheProperties.CACHE_ROLE_LIST);
    }

    @Transactional(readOnly = false)
    public void deleteRole(Role role) {
        roleMapper.deleteRoleMenu(role);
        roleMapper.delete(role);
        // 清除用户角色缓存
        //UserUtils.removeCache(UserUtils.CACHE_ROLE_LIST);
    }

    @Transactional(readOnly = false)
    public Boolean outUserInRole(Role role, User user) {
        List<Role> roles = user.getRoleList();
        for (Role e : roles) {
            if (e.getId().equals(role.getId())) {
                roles.remove(e);
                userService.saveUser(user);
                return true;
            }
        }
        return false;
    }

    @Transactional(readOnly = false)
    public User assignUserToRole(Role role, User user) {
        if (user == null) {
            return null;
        }
        List<String> roleIds = user.getRoleIdList();
        if (roleIds.contains(role.getId())) {
            return null;
        }
        user.getRoleList().add(role);
        userService.saveUser(user);
        return user;
    }


    public Page<Role> findPage(Page<Role> page, Role role) {
        role.setPage(page); // 设置参数
        page.setCount(roleMapper.findPageCount(role));
        page.setList(roleMapper.findList(role));
        return page;
    }
}
